/* internal/js/report/view.js */

/* Build Sales Per Time Period Report */

function buildSalesPerTimePeriodReport () {
	buildSalesPerTimePeriodReportValidate();
}

function buildSalesPerTimePeriodReportValidate () {
	var fromDate =  document.getElementById('fromDate2').value;
	var toDate = document.getElementById('toDate2').value;
	
	var rad = document.salesPerTimePeriodReportForm.timePeriod;
	var radioLength = rad.length;
	var timePeriod = '';
	for(var i = 0; i < radioLength; i++) {
		if(rad[i].checked) {
			timePeriod = rad[i].value;
		}
	}

	buildSalesPerTimePeriodReportSend(fromDate, toDate, timePeriod);
}

function buildSalesPerTimePeriodReportSend (fromDate, toDate, timePeriod) {
	var url = '/ordrs/internal/report';
	var params = 'a=' + encodeURI('2') +
				'&fromDate=' + encodeURI(fromDate) +
	            '&toDate=' + encodeURI(toDate) +
		        '&timePeriod=' + encodeURI(timePeriod);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				buildSalesPerTimePeriodReportReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				buildSalesPerTimePeriodReportReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function buildSalesPerTimePeriodReportReceive (xml) {
	if (xml.getElementsByTagName('report').length > 0) {
		var report = xml.getElementsByTagName('report')[0];
		showResults(buildSalesPerTimePeriodResultsTable(report));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Build Sales Per Time Period Report*/

/* Build Cost-Profit Report */

function generateCostProfitReport () {
        var fromDate = document.rptForm.fromDate3.value;
        var toDate = document.rptForm2.toDate3.value;
        //generateReportSend(fromDay, fromMonth, fromYear, toDay, toMonth, toYear);
        generateCostProfitReportSend(fromDate, toDate);
}

function generateCostProfitReportSend (fromDate, toDate) {
        var url = '/ordrs/internal/report';
        var params = 'a=' + encodeURI('3') +
                                 '&fromdate=' + encodeURI(fromDate) +
                                 '&todate=' + encodeURI(toDate);
        /*
                                 '&fromyear=' + encodeURI(fromYear) +
                                 '&today=' + encodeURI(toDay) +
                                 '&tomonth=' + encodeURI(toMonth) +
                                 '&toyear=' + encodeURI(toYear);
                                 
                                 */
        
        var req = getXmlHttpObject();
        req.onreadystatechange = function() {
                if (req.readyState == 4) {
                        if (req.status == 200) {
                                //returned code
                                generateCostProfitReportReceive(req.responseXML);
                        } else if (req.status == 204) {
                                //can't find server
                                generateCostProfitReportReceive(encodeErrorXML('-1', 'Cannot find server.'));
                        }
                }
        };
        sendPost(req, url, params);
}


function generateCostProfitReportReceive (xml) {
        if (xml.getElementsByTagName('menuitem-reports').length > 0) {
                var reports = xml.getElementsByTagName('menuitem-reports')[0];
                showResults(buildCostProfitTable(reports));
        } else{
                var code = getErrorCode(xml);
                var message = getErrorMessage(xml);
                if (code == 10201)
                        showResults(message);
                else if (code == -1)
                        showDialog(message);
                else
                        showDialog('Error ' + getErrorString(xml));
        }
}



/* END Build Cost-Profit Report */